vm.$watch('a.b.c', function (newVal, oldVal) { })

export default class Watcher {
  constructor(vm, expOrFn, cb) {
    this.vm = vm
    // 读取data.a.b.c的内容
    this.getter = parsePath(expOrFn)
    this.cb = cb
    this.value = this.get()
  }
  get() {
    window.target = this
    let value = this.getter.call(this.vm, this.vm)
    window.target = undefined
    return value
  }
  update() {
    const oldVal = this.value
    this.value = this.get()
    this.cb.call(this.vm, this.value, oldVal)
  }
}

// 解析简单路径
const bailRE = /[^\w.$]/
export function parsePath(path) {
  if (bailRE.test(path)) return
  const segments = path.split('.')
  return function (obj) {
    for (let i = 0; i < segments.length; i++) {
      if (!obj) return
      obj = obj[segments[i]]
    }
    return obj
  }
}